
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpaceAdventureLib.GamePlay;
using SpaceAdventureLib.Menus;

namespace SpaceAdventureLib
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class SpaceGame : TNCGameEngine.Game
    {              

        private MainMenu menu = null;
        public MainMenu Menu
        {
            get { return this.menu; }
            set { this.menu = value; }
        }

        private Campaign activeCampaign = null;
        public Campaign ActiveCampaign
        {
            get { return this.activeCampaign; }
            set { this.activeCampaign = value; }
        }

        private bool loadingStarted = false;

        public SpaceGame()
            : base()
        {

            this.PreInitialize(GameState.Loading);             
            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {

            base.Initialize();

            this.Content.RootDirectory = "Content";
            this.Graphics.SupportedOrientations = DisplayOrientation.Portrait;
            this.Graphics.PreferredBackBufferWidth = 800;
            this.Graphics.PreferredBackBufferHeight = 600;

            this.IsMouseVisible = false;
            //this.Graphics.IsFullScreen = true;

            this.menu = new MainMenu(this);
            this.menu.Initialize();
            
            this.activeCampaign = new Campaign(this);
            this.activeCampaign.Initialize();
            
        }

        public override void LoadingCompleted(int returnState)
        {
            this.CurrentGameState = returnState;
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {

            if (this.CurrentGameState != GameState.Loading)
            {

                // Allows the game to exit
                if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); }

                int state = this.CurrentGameState;

                switch (state)
                {

                    case GameState.Game:
                        this.activeCampaign.Update(gameTime);
                        break;

                    case GameState.Menu:
                        this.menu.Update(gameTime);
                        break;

                }

            }
            else
            {

                if (!this.loadingStarted)
                {
                    this.ContentLoader.QueueAllContents(this.activeCampaign.GetAssetsToLoad());
                    this.ContentLoader.StartLoading(this, GameState.Game);
                    this.loadingStarted = true;
                }

            }

            base.Update(gameTime);

        }



        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {

            if (!this.DrawLoadingScreen(gameTime))
            {

                this.GraphicsDevice.Clear(Color.CornflowerBlue);

                this.GraphicsDevice.BlendState = BlendState.Opaque;
                this.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                this.GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

                int state = this.CurrentGameState;

                switch (state)
                {

                    case GameState.Game:
                        this.activeCampaign.Draw(this.GraphicsDevice);
                        break;

                    case GameState.Menu:
                        this.menu.Draw(this.GraphicsDevice);
                        break;

                }

            }
            
            base.Draw(gameTime);

        }
    }

    public class GameState : TNCGameEngine.GameState
    {

        public const int Menu = 1;

    }

}
